package com.cn.file.index;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class IndexHeader {

    //第一个index的存储时间
    private final AtomicLong beginTimestamp = new AtomicLong(0);

    //最后一个index的存储时间
    private final AtomicLong endTimestamp = new AtomicLong(0);

    //第一个index的物理位置
    private final AtomicLong beginPhyOffset = new AtomicLong(0);

    //最后一个index的位置
    private final AtomicLong endPhyOffset = new AtomicLong(0);

    private final AtomicInteger hashSlotCount = new AtomicInteger(0);

    //index的总数
    private final AtomicInteger indexCount = new AtomicInteger(1);

    private final int beginTimestampLength = 0;
    private final int endtimestampLength = 8;
    private final int beginPhyOffsetLength = 16;
    private final int endPhyOffsetLength = 24;
    private final int hashSlotCountLength = 32;
    private final int indexCountLength = 36;

    private ByteBuffer byteBuffer;

    /**
     * 初始化
     *
     * @param byteBuffer
     */
    public IndexHeader(ByteBuffer byteBuffer) {
        this.byteBuffer = byteBuffer;
        if (this.byteBuffer != null) {
            this.beginTimestamp.set(this.byteBuffer.getLong(beginTimestampLength));
            this.endTimestamp.set(this.byteBuffer.getLong(endtimestampLength));
            this.beginPhyOffset.set(this.byteBuffer.getLong(beginPhyOffsetLength));
            this.endPhyOffset.set(this.byteBuffer.getLong(endPhyOffsetLength));
            this.hashSlotCount.set(this.byteBuffer.getInt(hashSlotCountLength));
            this.indexCount.set(this.byteBuffer.getInt(indexCountLength));
            if (this.indexCount.get() <= 0) {
                this.indexCount.set(1);
            }

        }
    }

    /**
     * 修改index总数
     *
     * @return
     */
    public int incrIndexCount() {
        return this.indexCount.incrementAndGet();
    }

    public int getIndexCount() {
        return this.indexCount.get();
    }

    //重置开始时间
    public void resetBeginTimestamp(long beginTimestamp) {
        this.beginTimestamp.set(beginTimestamp);
    }

    //重置开始位置
    public void resetBeginPhyoffset(long beginPhyOffset) {
        this.beginPhyOffset.set(beginPhyOffset);
    }

    /**
     * 最后一条index的时间
     *
     * @param endTimestamp
     */
    public void resetEndTimestamp(long endTimestamp) {
        this.endTimestamp.set(endTimestamp);
    }

    /**
     * 最后一条index的位移
     *
     * @param endPhyoffset
     */
    public void resetEndPhyoffset(long endPhyoffset) {
        this.endPhyOffset.set(endPhyoffset);
    }

    public void incrHashSlotnum() {
        this.hashSlotCount.incrementAndGet();
    }

    public long getBegintStamp() {
        return this.beginTimestamp.get();
    }

    public AtomicLong getBeginPhyOffset() {
        return beginPhyOffset;
    }

    public AtomicLong getEndPhyOffset() {
        return endPhyOffset;
    }

    public void flush() {
        this.byteBuffer.putLong(beginTimestampLength, this.beginTimestamp.get());
        this.byteBuffer.putLong(beginPhyOffsetLength, this.beginPhyOffset.get());
        this.byteBuffer.putLong(endtimestampLength, this.endTimestamp.get());
        this.byteBuffer.putLong(endPhyOffsetLength, this.endPhyOffset.get());
        this.byteBuffer.putInt(hashSlotCountLength, this.hashSlotCount.get());
        this.byteBuffer.putInt(indexCountLength, this.indexCount.get());
    }


}
